package com.yuyou.fn.updater.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.Measurement;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IMeasurementService;
import com.yuyou.fn.educational.vo.WhiltelistConfigVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@EventHandler(tables = {"yyfn.student_reg"})
@Component
public class WhilelistUpdater implements IEventHandler {


    @Resource
    private ICourseClassService courseClassService;


    @Resource
    private IMeasurementService measurementService;

    @Resource
    private RedisTemplate<String,String> redisTemplate ;

    @Override
    public void insert(String tableName, JSONArray rowList) {
        build(tableName, rowList);
    }

    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {
        //build(tableName, rowList);
    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

    }

    private void build(String tableName, JSONArray rowList) {
        for (int i = 0; i < rowList.size(); i++) {
            JSONObject row = rowList.getJSONObject(i);
            StudentReg studentReg = row.toJavaObject(StudentReg.class);

            CourseClass courseClass = courseClassService.findById(studentReg.getClassId());
            if(courseClass.getCampusId() == null ){
                return ;
            }
            Example<Measurement> example = Measurement.createExample();
            example.createCriteria().andEqualTo("areaTeamId" , studentReg.getAreaTeamId() ).andEqualTo("subjectId" ,courseClass.getSubjectId() );

            List<Measurement> measurements = measurementService.findListByExample(example);
            if(measurements == null || measurements.isEmpty() ){
                return ;
            }


            for(Measurement measurement : measurements ){

                if(StringUtils.isNotEmpty(measurement.getWhitelistConfig())){
                    WhiltelistConfigVo whiltelistConfigVo = JSONObject.parseObject(measurement.getWhitelistConfig(),WhiltelistConfigVo.class);

                    boolean ok  = false ;
                    List<Long> gradeIds = whiltelistConfigVo.getGradeIds();

                    if(gradeIds != null ){
                        for(Long id : gradeIds ){
                            if(id.equals(courseClass.getGradeId())){
                                ok = true ;
                                break;
                            }
                        }
                    }

                    if(!ok){
                        continue;
                    }

                    ok = false ;

                    List<Long> periodIds = whiltelistConfigVo.getPeriodIds();

                    if(periodIds != null ){
                        for(Long id : periodIds ){
                            if(id.equals(courseClass.getPeriodId())){
                                ok = true ;
                                break;
                            }
                        }
                    }

                    if(!ok){
                        continue;
                    }

                    ok = false ;

                    List<Integer> levels = whiltelistConfigVo.getLevels();
                    if(levels != null ){
                        for(Integer l : levels ){
                            if(l.equals(courseClass.getLevel())){
                                ok = true ;
                                break;
                            }
                        }
                    }

                    if(!ok){
                        continue;
                    }

                    ok = false ;

                    List<Long> campusIds = whiltelistConfigVo.getCampusIds();
                    if(campusIds != null ){
                        for(Long id : campusIds ){
                            if(id.equals(courseClass.getCampusId())){
                                ok = true ;
                                break;
                            }
                        }
                    }

                    if(ok){
                        redisTemplate.opsForHash().put(String.format("whiltelist:measurement:%d" , measurement.getMeasurementId() ) , String.valueOf(studentReg.getStudentId()),"Y");
                    }
                }
            }


        }
    }


}
